<template>
	<view class="content">
		<view class="search-box">
			<!--  -->

			<view class="input-box">
				<image class="searchImg" src="../../static/icons/lx/icon_navSearch2x.png" mode=""></image>
				<input class="searchInput" type="text" :adjust-position="true" :placeholder="defaultKeyword"
					@input="inputChange" v-model="keyword" @confirm="doSearch(false)"
					placeholder-class="placeholder-class" confirm-type="search">
				<image v-show="isShowKeyword" @tap="clearKeyword" class="searchImg searchCloseImg"
					src="../../static/icons/lx/Button_searchClose2x.png" mode=""></image>
			</view>

			<view class="search-btn" @tap="doSearch(false)">搜索</view>

			<!-- 原样式 end -->
		</view>
		<view class="search-keyword">
			<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
				<block v-for="(row,index) in keywordList" :key="index">
					<view class="keyword-entry" hover-class="keyword-entry-tap">
						<view class="keyword-text" @tap.stop="doSearch(keywordList[index].keyword)">
							<rich-text :nodes="row.htmlStr"></rich-text>
						</view>
						<!-- <view class="keyword-img" @tap="setKeyword(keywordList[index].keyword)">
							<image src="/static/icons/lx/back.png"></image>
						</view> -->
					</view>
				</block>

			</scroll-view>
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block" v-if="oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view>搜索历史</view>
						<view>
							<image @tap="oldDelete" src="../../static/icons/lx/icon_message_clear2x.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
				</view>
			</scroll-view>
			<!-- hot热门 -->
			<scroll-view class="hotlist" scroll-y="true">
				<view class="hotBlock">
					<view class="hotList">
						<view class="hotTitle">
							搜索热榜
						</view>
						<view class="hotListItem" v-for="item in hostListData" :key="item">
							{{item}}
						</view>
					</view>
					<view class="hotShopDiv">
						<view class="hotShopTitle">
							<image class="hotShopImg" src="../../static/icons/lx/icon_search_hotShop2x.png" mode="">
							</image>
							<text class="hotShopName">热搜店铺</text>
						</view>
						<view class="shopItem" v-for="(item, index) in hotShopData" :key="shop.Name">
							<template v-if="index==0">
								<view class="imgDiv">
									<image class="shopImg" :src="item.img_src" mode=""></image>
									<view class="leftTopNum">
										<text class="leftTopText">1</text>
									</view>
								</view>
								<view class="shopTextDiv">
									<text class="shopName">{{item.shopName}}</text>
									<view class="shopNumDiv">
										<text class="watchNumText">{{item.watchNum}}</text>
										<text class="watchInfoText">w人查看</text>
									</view>
								</view>
							</template>
							<!-- 2 -->
							<template v-else-if="index==1">
								<view class="imgDiv">
									<image class="shopImg" :src="item.img_src" mode=""></image>
									<view class="leftTopNum  leftTopBox2 ">
										<text class="leftTopText">2</text>
									</view>
								</view>
								<view class="shopTextDiv">
									<text class="shopName">{{item.shopName}}</text>
									<view class="shopNumDiv">
										<text class="watchNumText">{{item.watchNum}}</text>
										<text class="watchInfoText">w人查看</text>
									</view>
								</view>
							</template>
							<!-- 3 -->
							<template v-else-if="index==2">
								<view class="imgDiv">
									<image class="shopImg" :src="item.img_src" mode=""></image>
									<view class="leftTopNum leftTopBox3">
										<text class="leftTopText">3</text>
									</view>
								</view>
								<view class="shopTextDiv">
									<text class="shopName">{{item.shopName}}</text>
									<view class="shopNumDiv">
										<text class="watchNumText">{{item.watchNum}}</text>
										<text class="watchInfoText">w人查看</text>
									</view>
								</view>
							</template>
							<!-- >3 -->
							<template v-else>
								<view class="imgDiv">
									<image class="shopImg" :src="item.img_src" mode=""></image>
									<view class="leftTopNum ">
										<text class="leftTopText">3</text>
									</view>
								</view>
								<view class="shopTextDiv">
									<text class="shopName">{{item.shopName}}</text>
									<view class="shopNumDiv">
										<text class="watchNumText">{{item.watchNum}}</text>
										<text class="watchInfoText">w人查看</text>
									</view>
								</view>
							</template>
						</view>
					</view>
				</view>

			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				keywordList: [],
				isShowKeywordList: false,
				isShowKeyword: false, //显示删除关键字
				hostListData: ['汽车零部件', "汽车零部件2", "汽车零部件3",
					"汽车零部件4"
				],
				hotShopData: [{
						"img_src": "",
						"shopName": "四海兄弟汽车馆",
						"watchNum": '8.23'
					},
					{
						"img_src": "",
						"shopName": "四海兄弟汽车馆2",
						"watchNum": '8.23'
					},
					{
						"img_src": "",
						"shopName": "四海兄弟汽车馆3",
						"watchNum": '8.23'
					}

				]
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			init() {
				this.loadDefaultKeyword();
				this.loadOldKeyword();
			},
			blur() {
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "空调滤清器";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//加载热门搜索
			//监听输入
			inputChange(event) {
				// 当关键字不为空时显示删除按钮
				if (this.keyword) {
					this.isShowKeyword = true;
				} else {
					this.isShowKeyword = false;
				}
				//兼容引入组件时传入参数情况
				var keyword = event.detail ? event.detail.value : event;
				if (!keyword) {
					this.keywordList = [];
					this.isShowKeywordList = false;
					return;
				}
				this.isShowKeywordList = true;
				//以下示例截取淘宝的关键字，请替换成你的接口
				uni.request({
					url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
					success: (res) => {
						this.keywordList = [];
						this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);

					}
				});
			},
			//高亮关键字
			drawCorrelativeKeyword(keywords, keyword) {
				var len = keywords.length,
					keywordArr = [];
				for (var i = 0; i < len; i++) {
					var row = keywords[i];
					//定义高亮#9f9f9f
					var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
					html = '<div>' + html + '</div>';
					var tmpObj = {
						keyword: row[0],
						htmlStr: html
					};
					keywordArr.push(tmpObj)
				}
				return keywordArr;
			},
			//顶置关键字
			setKeyword(index) {
				this.keyword = this.keywordList[index].keyword;
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			//执行搜索
			doSearch(keyword) {
				keyword = keyword === false ? this.keyword : keyword;
				this.keyword = keyword;
				this.saveKeyword(keyword); //保存为历史 
				uni.showToast({
					title: keyword,
					icon: 'none',
					duration: 2000
				});
				uni.navigateTo({
					url: '../profqTwo/profqTwo?keyword='+keyword
				})
				//以下是示例跳转淘宝搜索，可自己实现搜索逻辑
				/*
				//#ifdef APP-PLUS
				plus.runtime.openURL(encodeURI('taobao://s.taobao.com/search?q=' + keyword));
				//#endif
				//#ifdef H5
				window.location.href = 'taobao://s.taobao.com/search?q=' + keyword
				//#endif
				*/
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							// 删除这个对应的，然后将新的加到数组头部
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			},
			clearKeyword() {
				this.keyword = '';
				this.isShowKeyword = false;
				// 点击清空后不显示搜索提示词
				this.isShowKeywordList = false;
			}
		}
	}
</script>
<style>
	view {
		display: block;
	}

	.search-box {
		width: 95%;
		background-color: #fff;
		padding: 15upx 2.5%;
		display: flex;
		justify-content: space-between;
		position: sticky;
		top: 0;
	}

	.search-box .mSearch-input-box {
		width: 100%;
	}

	.search-box .input-box {
		width: 85%;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-box .search-btn {
		width: 15%;
		margin: 0 0 0 2%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		font-size: 28upx;
		color: #fff;
		background: linear-gradient(to right, #ff9801, #ff570a);
		border-radius: 60upx;
	}

	.placeholder-class {
		color: #9e9e9e;
	}

	.search-keyword {
		height: calc(100vh - 88rpx);
		width: 100%;
		background-color: #fff;
	}

	.keyword-list-box {
		height: calc(100vh - 110upx);
		padding-top: 10upx;
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		width: 94%;
		height: 80upx;
		margin: 0 3%;
		font-size: 30upx;
		color: #333;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #e7e7e7;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80upx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: 400rpx;
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-box .keyword-block {
		padding: 10upx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		width: 94%;
		padding: 10upx 3%;
		font-size: 27upx;
		color: #333;
		display: flex;
		justify-content: space-between;
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 40upx;
		height: 40upx;
	}

	.keyword-box .keyword-block .keyword {
		width: 94%;
		padding: 3px 3%;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}


	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60upx;
		padding: 0 20upx;
		margin: 10upx 20upx 10upx 0;
		height: 60upx;
		font-size: 28upx;
		background-color: rgb(242, 242, 242);
		color: #6b6b6b;
	}

	/* start */
	.content {
		background-color: #fff;
	}

	.searchImg {
		width: 40rpx;
		height: 40rpx;
		border-right: solid 2rpx #5c7784;
	}

	.searchCloseImg {
		margin-right: 10rpx;
		border-right: none;
	}

	.input-box {
		border-radius: 36rpx;
		overflow: hidden;
		background-color: #f9f9f9;
	}

	.search-box .input-box>input {
		width: 83%;
		height: 60upx;
		font-size: 32upx;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		color: #6b6b6b;
	}

	/* 热搜榜 */
	.hotBlock {
		display: flex;
		justify-content: flex-start;
	}

	.hotList {
		margin-left: 30rpx;
		width: 120rpx;
		height: 300rpx;
	}

	.hotTitle {
		font-family: PingFang SC;
		font-size: 28rpx;
		line-height: 34rpx;
		color: #5c5c5c;
		margin-bottom: 16rpx;
	}

	.hotListItem {
		font-family: PingFang SC;
		font-size: 20rpx;
		line-height: 34rpx;
		color: #666666;
	}

	/* 店铺 */
	.hotShopDiv {
		margin-left: 72rpx;
	}

	.hotShopTitle {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 16rpx;
	}

	.hotShopImg {
		width: 40rpx;
		height: 40rpx;
	}

	.hotShopName {
		font-family: PingFang SC;
		font-size: 28rpx;
		line-height: 34rpx;
		color: #5c5c5c;
	}

	.shopItem {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 24rpx;
	}

	.imgDiv {
		position: relative;
		margin-right: 18rpx;
		background-color: skyblue;
		width: 84rpx;
		height: 84rpx;
		border-radius: 4rpx;
	}

	.leftTopNum {
		position: absolute;
		top: 0;
		left: 0;
		width: 24rpx;
		height: 24rpx;
		background-color: #ff1d1d;
		border-radius: 4rpx;
		line-height: 24rpx;
		text-align: center;
	}

	.leftTopBox2 {
		background-color: #f5610c;
	}

	.leftTopBox3 {
		background-color: #2175e3;
	}

	.leftTopText {
		font-family: PingFang SC;
		font-size: 20rpx;
		color: #ffffff;
	}

	.shopImg {
		width: 84rpx;
		height: 84rpx;
	}

	.shopTextDiv {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		height: 84rpx;
		width: 160rpx;
	}

	.shopName {
		font-family: PingFang SC;
		font-size: 20rpx;
		line-height: 34rpx;
		color: #5d5d5d;
	}

	.watchNumText {
		font-family: PingFang SC;
		font-size: 16rpx;
		line-height: 34rpx;
		color: #ec0d0d;
	}

	.watchInfoText {
		font-family: PingFang SC;
		font-size: 16rpx;
		letter-spacing: 0rpx;
		color: #919191;
	}
</style>
